local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "tablehelper"
local timer = require "timer"
local base = require "base"
local tabletool = require "tabletool"
local timetool = require "timetool"
require "dzpkenum"

local filename = "tabletimer.lua"

local TableTimer = {}

function TableTimer.doready(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	local seat = table_data.seats[request.seat_index]	
	if seat.rid ~= request.rid then
		return
	end
	if timerid ~= seat.ready_timer_id then
		return
	end
	seat.ready_timer_id = -1
	seat.ready_to_time = 0
	if table_data.conf.room_type == ERoomType.ROOM_TYPE_FRIEND then
		-----判断朋友桌局数到没有
		if seat ~= nil then
			if table_data.cur_games_times < table_data.conf.game_times then
				seat.state = ESeatState.SEAT_STATE_WAIT_START
				server.logic.tablelogic.gameready(table_data, request, seat)
				return
			end
			server.logic.tablelogic.passive_standuptable(table_data, seat)
		end
	else
		if seat.rid > 0 then
			server.logic.tablelogic.passive_standuptable(table_data, seat)
		end
	end
end

function TableTimer.waitstartcountdown(timerid)
	local server = msghelper:get_server()    
	local table_data = server.table_data
	if table_data.timer_id == timerid then
		table_data.timer_id = -1
		filelog.warn("------ game real start ------")
		server.logic.gamelogic.gamestart()
	end
end

function TableTimer.firsttodo(timerid)
	local server = msghelper:get_server()    
	local table_data = server.table_data
	if table_data.timer_id == timerid then
		table_data.timer_id = -1
		server.logic.gamelogic.todonotice()
    end
end

function TableTimer.showbalanceover(timerid)
	local server = msghelper:get_server()    
	local table_data = server.table_data
	if table_data.timer_id == timerid then
		table_data.timer_id = -1
		table_data.state = ETableState.TABLE_STATE_WAIT_ALL_READY
		table_data.gaming = false
		for _, seat in ipairs(table_data.seats) do
			if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER then
				seat.state = ESeatState.SEAT_STATE_WAIT_READY
				seat.is_ready = EBOOL.FALSE
				seat.ready_to_time = timetool.get_time() + table_data.conf.ready_timeout
				-- local outoftimermsg = {
				-- 	rid = seat.rid,
				-- 	seat_index = seat.index,
				-- }
				-- seat.ready_timer_id = timer.settimer(table_data.conf.ready_timeout*100, "doready", outoftimermsg)
				local doreadyntcmsg = {
					rid = seat.rid,
					seat_index = seat.index,
					ready_to_time = seat.ready_to_time,
					is_ready = EBOOL.FALSE,
				}
				msghelper:sendmsg_to_alltableplayer("doreadyN", doreadyntcmsg)
			end
		end
    end
end

function TableTimer.delete_table(timerid, request)
	local server = msghelper:get_server()    
	local table_data = server.table_data
	if table_data.delete_table_timer_id == timerid then
		for _, seat in ipairs(table_data.seats) do
			if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER and seat.rid > 0 then
				server.logic.tablelogic.passive_standuptable(table_data, seat)
			end
		end
		table_data.delete_table_timer_id = -1
        server.delete()
    end
end

return TableTimer